﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using FFmpegVideoClip.Configuration;

namespace FFmpegVideoClip.Lib.UtilityControlsTools
{
    [ValueConversion(typeof(double[]), typeof(string))]
    public class ZoomLevelValueConvertString : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value is double[] result)
                return string.Join(";", result.Select(p => p.ToString()));
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string result && string.IsNullOrWhiteSpace(result))
            {
                Span<string> StringValues = result.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                if (StringValues.Length < 1)
                    return Config.Instance.ZoomLevelValuesSeconds;

                List<double> Values = new(StringValues.Length);
                foreach (ref string StringValue in StringValues)
                    if (double.TryParse(StringValue, out double TimeValue))
                        Values.Add(TimeValue);
                if(Values.Count > 0)
                    return Values.ToArray();

                return Config.Instance.ZoomLevelValuesSeconds;
            }
            return Config.Instance.ZoomLevelValuesSeconds;

        }
    }
}
